package com.interfaces.dao;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.ClassUtils;
/**
 * 
 * <p>功能描述:ibatis工具类</p>
 * @author  <a href="mailto: zhuyoulong@gtss.com.cn">zhuyoulong</a>
 * @create_date 2008-10-26
 * @version $ Revision: 1.0
 */
public final class IBatisDaoUtils
{

    private IBatisDaoUtils()
    {
    }
    /**
     * 
     * <p>功能描述：获得主键id</p>
     * @param o
     * @return
     * @author:zhuyoulong
     * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    protected static String getPrimaryKeyFieldName(Object o)
    {
        Field fieldlist[] = o.getClass().getDeclaredFields();
        String fieldName = null;
        Field arr$[] = fieldlist;
        int len$ = arr$.length;
        int i$ = 0;
        do
        {
            if(i$ >= len$)
                break;
            Field fld = arr$[i$];
            if(fld.getName().equals("id") || fld.getName().indexOf("Id") > -1 || fld.getName().equals("version"))
            {
                fieldName = fld.getName();
                break;
            }
            i$++;
        } while(true);
        return fieldName;
    }
    /**
     * 
     * <p>功能描述：方法功能描述
     * </p>
     * @param o
     * @return
     * @author:zhuyoulong
     * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    public static Class getPrimaryKeyFieldType(Object o)
    {
        Field fieldlist[] = o.getClass().getDeclaredFields();
        Class fieldType = null;
        Field arr$[] = fieldlist;
        int len$ = arr$.length;
        int i$ = 0;
        do
        {
            if(i$ >= len$)
                break;
            Field fld = arr$[i$];
            if(fld.getName().equals("id") || fld.getName().indexOf("Id") > -1 || fld.getName().equals("version"))
            {
                fieldType = fld.getType();
                break;
            }
            i$++;
        } while(true);
        return fieldType;
    }
    /**
     * 
     * <p>功能描述：方法功能描述
     * </p>
     * @param o
     * @return
     * @author:zhuyoulong
     * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    public static Object getPrimaryKeyValue(Object o)
    {
        String getterMethod="";
        try{
	        String fieldName = getPrimaryKeyFieldName(o);
	        //获得主键get方法名
	        getterMethod = (new StringBuilder()).append("get").append(Character.toUpperCase(fieldName.charAt(0))).append(fieldName.substring(1)).toString();
	        Method getMethod = o.getClass().getMethod(getterMethod, (Class[])null);
	        return getMethod.invoke(o, (Object[])null);//动态调用对象的的get方法
        }
        catch(Exception e){
        	e.printStackTrace();
        	log.error((new StringBuilder()).append("Could not invoke method '").append(getterMethod).append("' on ").append(ClassUtils.getShortName(o.getClass())).toString());        
        	return null;
        }
    }
    /**
     * 
     * <p>功能描述： </p>
     * @param o
     * @author:zhuyoulong
     * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    public static void prepareObjectForSaveOrUpdate(Object o)
    {
        try
        {
            Field fieldlist[] = o.getClass().getDeclaredFields();
            Field arr$[] = fieldlist;
            int len$ = arr$.length;
            for(int i$ = 0; i$ < len$; i$++)
            {
                Field fld = arr$[i$];
                String fieldName = fld.getName();
                if(fieldName.equals("version"))
                {
                    Method setMethod = o.getClass().getMethod("setVersion", new Class[] {java.lang.Integer.class});
                    Object value = o.getClass().getMethod("getVersion", (Class[])null).invoke(o, (Object[])null);
                    if(value == null)
                        setMethod.invoke(o, new Object[] {
                            Integer.valueOf(1)
                        });
                    else
                        setMethod.invoke(o, new Object[] {
                            Integer.valueOf(((Integer)value).intValue() + 1)
                        });
                }
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            log.error((new StringBuilder()).append("Could not prepare '").append(ClassUtils.getShortName(o.getClass())).append("' for insert/update").toString());
        }
    }
    /**
     * 
     * <p>功能描述：方法功能描述
     * 
     * </p>
     * @param o
     * @param clazz
     * @param value
     * @author:zhuyoulong
     * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
     */
    public static void setPrimaryKey(Object o, Class clazz, Object value)
    {
        String fieldName = getPrimaryKeyFieldName(o);
        String setMethodName = (new StringBuilder()).append("set").append(Character.toUpperCase(fieldName.charAt(0))).append(fieldName.substring(1)).toString();
        try
        {
            Method setMethod = o.getClass().getMethod(setMethodName, new Class[] {
                clazz
            });
            if(value != null)
                setMethod.invoke(o, new Object[] {
                    value
                });
        }
        catch(Exception e)
        {
            e.printStackTrace();
            log.error(MessageFormat.format("Could not set ''{0}.{1} with value {2}", new Object[] {
                ClassUtils.getShortName(o.getClass()), fieldName, value
            }));
        }
    }

    public static String getSelectQuery(String className)
    {
        return (new StringBuilder()).append("get").append(className).append("s").toString();
    }
    
    public static String getObjectList(String className)
    {
        return (new StringBuilder()).append("get").append(className).append("List").toString();
    }

    public static String getFindQuery(String className)
    {
        return (new StringBuilder()).append("get").append(className).toString();
    }

    public static String getInsertQuery(String className)
    {
        return (new StringBuilder()).append("add").append(className).toString();
    }

    public static String getUpdateQuery(String className)
    {
        return (new StringBuilder()).append("update").append(className).toString();
    }

    public static String getDeleteQuery(String className)
    {
        return (new StringBuilder()).append("delete").append(className).toString();
    }
    
    public static String getCount(String className)
    {
        return (new StringBuilder()).append("get").append(className).append("Count").toString();
    }
    public static String getCount1(String className){
    	return (new StringBuilder()).append("get").append(className).append("Count1").toString();
    }
    protected static final Log log = LogFactory.getLog(IBatisDaoUtils.class);
}
